package com.maxjay.common.frame.hibernate.keygenerator;

import java.io.Serializable;
import java.util.Properties;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.type.Type;

public class UUIDGenerator implements IdentifierGenerator, Configurable{
	
	private String uuidPrefix;

	public Serializable generate(SessionImplementor sessionimplementor, Object obj) throws HibernateException {
		String uuid = UUID.randomUUID().toString();
		String key = "";
		if(StringUtils.isNotBlank(uuidPrefix)){
			key = uuidPrefix + uuid;
			key = key.replaceAll("-", "");
		} else {
			key = uuid;
		}
		return key.toUpperCase();
	}

	public void configure(Type type, Properties properties, Dialect dialect) throws MappingException {
		uuidPrefix = properties.getProperty("prefix");
	}
	
	public static void main(String[] args){
		UUIDGenerator gen = new UUIDGenerator();
		gen.uuidPrefix = "XMBB";
		System.out.println(gen.generate(null, null));
	}

}
